library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.5
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmltools)
library(devtools)
## Warning: package 'devtools' was built under R version 4.0.5
## Loading required package: usethis
## Warning: package 'usethis' was built under R version 4.0.5
library(caret)
## Warning: package 'caret' was built under R version 4.0.5
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
library(NbClust)
#Read in data

nba1 <- read.csv("C:/Users/Maddie/OneDrive/Desktop/3YEAR/Forked-DS-3001/data/nba_salaries_21.csv")
nba2 <- read.csv("C:/Users/Maddie/OneDrive/Desktop/3YEAR/Forked-DS-3001/data/nba2020-21.csv")

nba <- merge(nba1, nba2)
nba <- na.exclude(nba)

Variable selection: In order to determine which players are high performing but not as highly paid, I will be using clustering to understand the salary variable. My variables of choice here will be the minutes played, threes scored and total points scored, as I believe that a good player will play more minutes, score more threes, and have an overall greater points scored total.

#Normalize data

normalize <- function(x){
 (x - min(x)) / (max(x) - min(x))
}

nba$minutes_played <- normalize(nba$MP)
nba$threes <- normalize(nba$X3PA)
nba$points <- normalize(nba$PTS)

#cut salary
#summary(nba$X2020.21)
#nba$salary <- cut(nba$X2020.21, c(0, 2113500,  4568480, 11992265, 43006362),labels = c("Lowest", "Low", "High", "Highest"))
nba$salary <- nba$X2020.21

#Select clustering data!
clust_nba = nba[, c(31:33)]
View(clust_nba)
#Run the clustering algorithm with 2 centers
set.seed(1)
kmeans_obj = kmeans(clust_nba, centers = 2, 
                        algorithm = "Lloyd")

kmeans_obj
## K-means clustering with 2 clusters of sizes 249, 169
## 
## Cluster means:
##   minutes_played    threes    points
## 1      0.2657732 0.1086198 0.1295697
## 2      0.6959709 0.3664454 0.4671288
## 
## Clustering vector:
##   1   2   3   4   5   6   7   9  10  11  12  13  14  15  16  17  18  19  20  21 
##   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   2   1   1   2   1 
##  22  23  24  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 
##   2   2   1   1   1   1   2   1   2   1   1   2   1   1   2   1   1   2   2   1 
##  43  44  45  46  47  48  49  50  51  52  53  54  55  57  58  59  60  61  62  63 
##   1   1   2   2   1   2   2   1   1   2   1   2   1   2   2   1   1   1   1   1 
##  65  66  67  68  69  70  72  73  74  75  76  77  79  80  81  83  84  85  86  87 
##   2   1   1   1   2   1   2   1   1   2   1   1   2   2   1   1   2   1   2   1 
##  88  89  90  91  92  93  94  95  96  97  98  99 100 101 103 104 105 106 107 108 
##   1   1   1   2   2   1   1   2   1   1   1   1   2   1   2   2   2   1   1   2 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   1   1   2   1   1   1   1   1   2   1   2   2   2   2   2   2   2 
## 129 131 132 133 134 135 136 138 139 140 141 143 144 145 146 147 149 150 151 152 
##   2   2   2   1   1   1   1   1   2   2   1   2   2   1   2   1   1   1   2   1 
## 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 173 174 
##   2   1   1   2   1   1   2   1   1   1   2   1   1   1   2   2   1   1   2   1 
## 175 177 178 179 180 181 182 183 185 186 187 188 190 191 193 194 195 196 197 198 
##   2   1   1   1   1   1   1   2   1   2   2   1   1   1   2   1   1   1   2   1 
## 199 200 201 202 203 204 206 208 209 210 211 212 214 215 216 217 218 219 220 221 
##   2   2   1   1   1   1   1   1   2   1   1   1   2   1   2   2   1   2   1   1 
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
##   1   2   2   2   2   2   1   2   2   2   1   1   1   2   2   1   2   2   1   2 
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 
##   2   1   1   1   1   2   1   2   2   2   2   2   1   1   2   1   2   2   1   1 
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 
##   1   1   2   1   1   2   2   2   2   2   1   1   2   2   1   1   1   2   2   2 
## 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 
##   2   2   2   1   1   1   1   2   2   1   1   2   1   1   1   1   2   2   1   1 
## 302 303 305 306 307 308 309 310 311 312 314 315 316 317 318 319 320 321 322 323 
##   1   1   1   1   2   2   2   2   1   2   1   1   2   2   1   1   2   1   2   1 
## 324 326 327 328 329 330 331 332 334 338 339 340 341 342 343 344 345 346 348 349 
##   1   1   2   2   1   1   2   2   2   1   2   1   1   2   2   2   1   1   1   1 
## 350 351 352 353 355 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 
##   2   2   2   1   1   1   1   1   1   1   2   2   2   2   2   2   1   1   1   1 
## 372 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 
##   1   1   1   2   1   2   2   2   1   2   1   1   1   2   2   2   2   1   1   1 
## 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 410 411 412 413 
##   1   1   2   2   2   1   1   1   1   1   1   1   1   1   1   1   2   2   1   1 
## 414 415 416 417 418 419 420 421 423 424 425 426 427 428 429 431 432 433 434 435 
##   2   1   1   2   2   2   1   1   1   2   1   1   1   1   1   2   1   1   2   1 
## 436 438 439 440 441 443 444 445 446 448 449 450 451 452 453 454 455 456 
##   1   1   2   1   1   1   2   2   1   1   1   2   1   1   1   2   1   2 
## 
## Within cluster sum of squares by cluster:
## [1] 10.55041 14.43537
##  (between_SS / total_SS =  59.6 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
# we can see here that the ratio with 2 centers is only 59.6%... not so great.

#Determine optimal number of clusters

#Method 1: Elbow Graph
explained_variance = function(data_in, k){
  set.seed(1)
  kmeans_obj = kmeans(data_in, centers = k, algorithm = "Lloyd", iter.max = 30)
  var_exp = kmeans_obj$betweenss / kmeans_obj$totss
  var_exp  
}

explained_var = sapply(1:10, explained_variance, data_in = clust_nba)
explained_var
##  [1] -2.300217e-16  5.955718e-01  7.636619e-01  8.236263e-01  8.452990e-01
##  [6]  8.729823e-01  8.847162e-01  8.963012e-01  9.087699e-01  9.172521e-01
elbow_data = data.frame(k = 1:10, explained_var)

ggplot(elbow_data, 
       aes(x = k,  
           y = explained_var)) + 
  geom_point(size = 4) +           #<- sets the size of the data points
  geom_line(size = 1) +            #<- sets the thickness of the line
  xlab('k') + 
  ylab('Inter-cluster Variance / Total Variance') + 
  theme_light()

#Method 2: Nbclust
(nbclust_obj = NbClust(data = clust_nba, method = "kmeans"))

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 11 proposed 3 as the best number of clusters 
## * 3 proposed 6 as the best number of clusters 
## * 1 proposed 11 as the best number of clusters 
## * 3 proposed 14 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  3 
##  
##  
## *******************************************************************
## $All.index
##         KL       CH Hartigan     CCC    Scott  Marriot  TrCovW  TraceW Friedman
## 2   2.3275 612.7018 300.9257 16.4789 1083.796 907.2900 17.7780 24.9836  35.8671
## 3   3.5072 676.7907 137.3770 11.7649 1542.827 680.7745 13.8595 14.4969  51.0116
## 4   5.7413 644.7869  58.2820  9.5536 1830.505 608.1188 12.9204 10.8915  63.8479
## 5   0.2331 564.8717  89.8513 10.4483 2007.665 621.9325 12.3144  9.5474  76.4127
## 6  13.6605 566.8056  41.9930 11.4142 2213.429 547.4201  5.3379  7.8414  86.3529
## 7   0.3528 526.1995  45.6331 10.7911 2323.625 572.4303  4.3303  7.1161  89.2200
## 8   0.2484 506.3894  64.8391 10.8040 2440.365 565.4779  4.0481  6.4050 100.3913
## 9   5.9395 519.9964  35.0456 12.0494 2604.542 483.2189  2.9495  5.5304 109.7982
## 10 12.6849 504.4823  27.1789 14.8607 2703.263 471.0734  2.3613  5.0939 115.5954
## 11  0.0738 485.8038  18.8263 14.6919 2787.448 466.0233  2.7911  4.7758 131.0448
## 12  0.7490 462.6404  15.1091 14.1710 2846.244 481.8339  2.1047  4.5646 132.5141
## 13  0.4305 440.0418  37.6675 13.5536 2900.523 496.6227  1.8755  4.4009 142.8455
## 14  1.6113 445.7649   8.6900 14.4179 2995.011 459.4347  1.7251  4.0264 150.1588
## 15  1.1613 422.4006   9.0567 13.6058 3012.411 505.9084  1.7097  3.9416 147.9341
##      Rubin Cindex     DB Silhouette   Duda  Pseudot2   Beale Ratkowsky    Ball
## 2   7.6499 0.2290 0.8240     0.4960 0.6452  122.0719  0.9323    0.5341 12.4918
## 3  13.1837 0.2322 0.8079     0.4779 1.7870 -102.6127 -0.7451    0.4969  4.8323
## 4  17.5479 0.1909 0.9724     0.4158 1.3710  -50.0581 -0.4570    0.4463  2.7229
## 5  20.0182 0.1764 1.1087     0.3509 1.6106  -49.6659 -0.6375    0.4038  1.9095
## 6  24.3734 0.1557 0.9737     0.3875 2.1933  -51.1425 -0.9065    0.3775  1.3069
## 7  26.8576 0.1718 1.0802     0.3662 1.8122  -43.9232 -0.7503    0.3528  1.0166
## 8  29.8396 0.1603 1.0796     0.3571 1.8521  -34.5059 -0.7679    0.3322  0.8006
## 9  34.5586 0.2129 1.0925     0.3484 1.1954  -22.2325 -0.2731    0.3166  0.6145
## 10 37.5198 0.2054 1.0458     0.3544 1.3054  -24.5671 -0.3912    0.3018  0.5094
## 11 40.0191 0.1957 1.0975     0.3327 3.0700  -53.2667 -1.1258    0.2881  0.4342
## 12 41.8703 0.1919 1.0586     0.3394 0.6545   52.2529  0.8836    0.2768  0.3804
## 13 43.4285 0.1862 1.1019     0.3340 2.4011  -26.8424 -0.9566    0.2663  0.3385
## 14 47.4676 0.1738 1.1084     0.3358 0.8873   10.1582  0.2100    0.2575  0.2876
## 15 48.4886 0.1711 1.1076     0.3302 2.0366  -21.3770 -0.8123    0.2491  0.2628
##    Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex   SDbw
## 2      0.6066  0.5575  0.4239 0.0125 0.0190 15.3590 0.2176 1.1924
## 3      0.6371  1.1726  0.5935 0.0198 0.0259 12.5979 0.1667 0.6186
## 4      0.5684  1.6285  0.9111 0.0244 0.0279 14.3611 0.1407 0.3683
## 5      0.5117  0.5398  1.1968 0.0137 0.0292 16.8583 0.1295 0.4811
## 6      0.4905  0.9524  1.3429 0.0178 0.0299 16.4303 0.1165 0.3277
## 7      0.4634  0.4620  1.5240 0.0155 0.0304 18.9462 0.1120 0.4136
## 8      0.4519  0.2632  1.5995 0.0155 0.0307 18.9611 0.1052 0.2449
## 9      0.4474  0.4003  1.6115 0.0221 0.0315 18.9650 0.1009 0.2768
## 10     0.4424  1.5713  1.6364 0.0324 0.0317 20.0917 0.0967 0.3136
## 11     0.4038  0.0179  1.9677 0.0411 0.0323 22.8788 0.0917 0.2092
## 12     0.4049  1.2149  1.9435 0.0429 0.0322 23.3105 0.0895 0.1962
## 13     0.3857  0.2018  2.1340 0.0429 0.0321 30.0514 0.0874 0.2003
## 14     0.3817  0.9964  2.1239 0.0431 0.0324 30.0719 0.0840 0.1498
## 15     0.3738 -7.8256  2.2059 0.0367 0.0324 29.1627 0.0827 0.1743
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.6455           121.9294       0.4246
## 3          0.6119           147.7930       1.0000
## 4          0.5889           129.1215       1.0000
## 5          0.5413           111.0092       1.0000
## 6          0.4656           107.8810       1.0000
## 7          0.5020            97.2171       1.0000
## 8          0.4783            81.8185       1.0000
## 9          0.4868           143.3640       1.0000
## 10         0.4921           108.3507       1.0000
## 11         0.4812            85.1750       1.0000
## 12         0.5020            98.2091       0.4508
## 13         0.3660            79.6932       1.0000
## 14         0.4158           112.4092       0.8893
## 15         0.2464           128.4303       1.0000
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott  Marriot TrCovW
## Number_clusters  6.0000   3.0000   3.0000  2.0000   3.0000   3.0000 6.0000
## Value_Index     13.6605 676.7907 163.5487 16.4789 459.0314 153.8597 6.9765
##                 TraceW Friedman   Rubin Cindex     DB Silhouette  Duda
## Number_clusters 3.0000  11.0000 14.0000 6.0000 3.0000      2.000 3.000
## Value_Index     6.8813  15.4494 -3.0181 0.1557 0.8079      0.496 1.787
##                  PseudoT2  Beale Ratkowsky   Ball PtBiserial Frey McClain
## Number_clusters    3.0000 2.0000    2.0000 3.0000     3.0000    1  2.0000
## Value_Index     -102.6127 0.9323    0.5341 7.6595     0.6371   NA  0.4239
##                    Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 14.0000      0  3.0000      0 14.0000
## Value_Index      0.0431      0 12.5979      0  0.1498
## 
## $Best.partition
##   1   2   3   4   5   6   7   9  10  11  12  13  14  15  16  17  18  19  20  21 
##   2   2   1   1   1   2   2   2   1   1   1   1   1   1   1   2   2   1   3   2 
##  22  23  24  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 
##   2   3   1   2   1   1   2   1   2   2   2   2   1   2   3   2   1   3   2   2 
##  43  44  45  46  47  48  49  50  51  52  53  54  55  57  58  59  60  61  62  63 
##   1   1   2   3   1   2   2   1   1   2   1   2   1   2   3   1   2   1   1   2 
##  65  66  67  68  69  70  72  73  74  75  76  77  79  80  81  83  84  85  86  87 
##   3   1   1   2   3   2   2   2   1   2   1   1   3   2   2   1   2   2   2   1 
##  88  89  90  91  92  93  94  95  96  97  98  99 100 101 103 104 105 106 107 108 
##   2   1   1   3   2   1   2   3   2   1   1   1   2   2   2   2   2   2   2   2 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   1   2   1   2   1   1   2   3   1   2   2   3   3   2   2   2 
## 129 131 132 133 134 135 136 138 139 140 141 143 144 145 146 147 149 150 151 152 
##   2   3   2   2   1   1   1   1   2   2   1   3   2   2   2   2   1   1   3   2 
## 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 173 174 
##   2   2   2   3   1   2   3   1   1   2   3   1   1   2   2   3   1   1   2   1 
## 175 177 178 179 180 181 182 183 185 186 187 188 190 191 193 194 195 196 197 198 
##   2   1   2   2   1   2   2   2   2   2   2   1   1   1   2   1   1   1   3   1 
## 199 200 201 202 203 204 206 208 209 210 211 212 214 215 216 217 218 219 220 221 
##   3   3   1   1   2   2   2   2   2   1   1   1   3   1   3   2   1   3   2   1 
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
##   1   2   3   2   3   3   1   2   2   3   1   1   1   2   2   2   2   2   1   3 
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 
##   3   1   1   1   2   3   1   2   2   3   2   2   2   2   2   1   2   3   1   1 
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 
##   1   2   3   1   1   2   2   2   3   3   1   2   3   2   1   2   2   3   2   3 
## 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 
##   2   3   3   1   2   1   1   3   3   2   2   2   2   1   2   1   2   2   2   1 
## 302 303 305 306 307 308 309 310 311 312 314 315 316 317 318 319 320 321 322 323 
##   1   2   1   1   2   3   2   2   1   2   1   1   2   2   1   1   3   1   2   1 
## 324 326 327 328 329 330 331 332 334 338 339 340 341 342 343 344 345 346 348 349 
##   2   2   2   2   1   1   3   3   3   1   2   1   1   2   2   3   2   2   1   1 
## 350 351 352 353 355 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 
##   2   2   3   2   2   1   1   1   1   2   2   2   2   2   3   2   1   1   2   1 
## 372 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 
##   1   2   1   2   2   2   2   2   1   2   1   1   2   2   2   3   2   1   1   2 
## 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 410 411 412 413 
##   1   1   3   2   2   2   2   1   1   1   2   1   1   2   1   2   3   3   2   1 
## 414 415 416 417 418 419 420 421 423 424 425 426 427 428 429 431 432 433 434 435 
##   2   1   1   3   2   3   1   1   1   3   1   2   1   2   1   2   1   1   2   2 
## 436 438 439 440 441 443 444 445 446 448 449 450 451 452 453 454 455 456 
##   2   1   2   2   1   1   2   2   2   1   2   2   1   1   2   3   1   3
nbclust_obj
## $All.index
##         KL       CH Hartigan     CCC    Scott  Marriot  TrCovW  TraceW Friedman
## 2   2.3275 612.7018 300.9257 16.4789 1083.796 907.2900 17.7780 24.9836  35.8671
## 3   3.5072 676.7907 137.3770 11.7649 1542.827 680.7745 13.8595 14.4969  51.0116
## 4   5.7413 644.7869  58.2820  9.5536 1830.505 608.1188 12.9204 10.8915  63.8479
## 5   0.2331 564.8717  89.8513 10.4483 2007.665 621.9325 12.3144  9.5474  76.4127
## 6  13.6605 566.8056  41.9930 11.4142 2213.429 547.4201  5.3379  7.8414  86.3529
## 7   0.3528 526.1995  45.6331 10.7911 2323.625 572.4303  4.3303  7.1161  89.2200
## 8   0.2484 506.3894  64.8391 10.8040 2440.365 565.4779  4.0481  6.4050 100.3913
## 9   5.9395 519.9964  35.0456 12.0494 2604.542 483.2189  2.9495  5.5304 109.7982
## 10 12.6849 504.4823  27.1789 14.8607 2703.263 471.0734  2.3613  5.0939 115.5954
## 11  0.0738 485.8038  18.8263 14.6919 2787.448 466.0233  2.7911  4.7758 131.0448
## 12  0.7490 462.6404  15.1091 14.1710 2846.244 481.8339  2.1047  4.5646 132.5141
## 13  0.4305 440.0418  37.6675 13.5536 2900.523 496.6227  1.8755  4.4009 142.8455
## 14  1.6113 445.7649   8.6900 14.4179 2995.011 459.4347  1.7251  4.0264 150.1588
## 15  1.1613 422.4006   9.0567 13.6058 3012.411 505.9084  1.7097  3.9416 147.9341
##      Rubin Cindex     DB Silhouette   Duda  Pseudot2   Beale Ratkowsky    Ball
## 2   7.6499 0.2290 0.8240     0.4960 0.6452  122.0719  0.9323    0.5341 12.4918
## 3  13.1837 0.2322 0.8079     0.4779 1.7870 -102.6127 -0.7451    0.4969  4.8323
## 4  17.5479 0.1909 0.9724     0.4158 1.3710  -50.0581 -0.4570    0.4463  2.7229
## 5  20.0182 0.1764 1.1087     0.3509 1.6106  -49.6659 -0.6375    0.4038  1.9095
## 6  24.3734 0.1557 0.9737     0.3875 2.1933  -51.1425 -0.9065    0.3775  1.3069
## 7  26.8576 0.1718 1.0802     0.3662 1.8122  -43.9232 -0.7503    0.3528  1.0166
## 8  29.8396 0.1603 1.0796     0.3571 1.8521  -34.5059 -0.7679    0.3322  0.8006
## 9  34.5586 0.2129 1.0925     0.3484 1.1954  -22.2325 -0.2731    0.3166  0.6145
## 10 37.5198 0.2054 1.0458     0.3544 1.3054  -24.5671 -0.3912    0.3018  0.5094
## 11 40.0191 0.1957 1.0975     0.3327 3.0700  -53.2667 -1.1258    0.2881  0.4342
## 12 41.8703 0.1919 1.0586     0.3394 0.6545   52.2529  0.8836    0.2768  0.3804
## 13 43.4285 0.1862 1.1019     0.3340 2.4011  -26.8424 -0.9566    0.2663  0.3385
## 14 47.4676 0.1738 1.1084     0.3358 0.8873   10.1582  0.2100    0.2575  0.2876
## 15 48.4886 0.1711 1.1076     0.3302 2.0366  -21.3770 -0.8123    0.2491  0.2628
##    Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex   SDbw
## 2      0.6066  0.5575  0.4239 0.0125 0.0190 15.3590 0.2176 1.1924
## 3      0.6371  1.1726  0.5935 0.0198 0.0259 12.5979 0.1667 0.6186
## 4      0.5684  1.6285  0.9111 0.0244 0.0279 14.3611 0.1407 0.3683
## 5      0.5117  0.5398  1.1968 0.0137 0.0292 16.8583 0.1295 0.4811
## 6      0.4905  0.9524  1.3429 0.0178 0.0299 16.4303 0.1165 0.3277
## 7      0.4634  0.4620  1.5240 0.0155 0.0304 18.9462 0.1120 0.4136
## 8      0.4519  0.2632  1.5995 0.0155 0.0307 18.9611 0.1052 0.2449
## 9      0.4474  0.4003  1.6115 0.0221 0.0315 18.9650 0.1009 0.2768
## 10     0.4424  1.5713  1.6364 0.0324 0.0317 20.0917 0.0967 0.3136
## 11     0.4038  0.0179  1.9677 0.0411 0.0323 22.8788 0.0917 0.2092
## 12     0.4049  1.2149  1.9435 0.0429 0.0322 23.3105 0.0895 0.1962
## 13     0.3857  0.2018  2.1340 0.0429 0.0321 30.0514 0.0874 0.2003
## 14     0.3817  0.9964  2.1239 0.0431 0.0324 30.0719 0.0840 0.1498
## 15     0.3738 -7.8256  2.2059 0.0367 0.0324 29.1627 0.0827 0.1743
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.6455           121.9294       0.4246
## 3          0.6119           147.7930       1.0000
## 4          0.5889           129.1215       1.0000
## 5          0.5413           111.0092       1.0000
## 6          0.4656           107.8810       1.0000
## 7          0.5020            97.2171       1.0000
## 8          0.4783            81.8185       1.0000
## 9          0.4868           143.3640       1.0000
## 10         0.4921           108.3507       1.0000
## 11         0.4812            85.1750       1.0000
## 12         0.5020            98.2091       0.4508
## 13         0.3660            79.6932       1.0000
## 14         0.4158           112.4092       0.8893
## 15         0.2464           128.4303       1.0000
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott  Marriot TrCovW
## Number_clusters  6.0000   3.0000   3.0000  2.0000   3.0000   3.0000 6.0000
## Value_Index     13.6605 676.7907 163.5487 16.4789 459.0314 153.8597 6.9765
##                 TraceW Friedman   Rubin Cindex     DB Silhouette  Duda
## Number_clusters 3.0000  11.0000 14.0000 6.0000 3.0000      2.000 3.000
## Value_Index     6.8813  15.4494 -3.0181 0.1557 0.8079      0.496 1.787
##                  PseudoT2  Beale Ratkowsky   Ball PtBiserial Frey McClain
## Number_clusters    3.0000 2.0000    2.0000 3.0000     3.0000    1  2.0000
## Value_Index     -102.6127 0.9323    0.5341 7.6595     0.6371   NA  0.4239
##                    Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 14.0000      0  3.0000      0 14.0000
## Value_Index      0.0431      0 12.5979      0  0.1498
## 
## $Best.partition
##   1   2   3   4   5   6   7   9  10  11  12  13  14  15  16  17  18  19  20  21 
##   2   2   1   1   1   2   2   2   1   1   1   1   1   1   1   2   2   1   3   2 
##  22  23  24  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 
##   2   3   1   2   1   1   2   1   2   2   2   2   1   2   3   2   1   3   2   2 
##  43  44  45  46  47  48  49  50  51  52  53  54  55  57  58  59  60  61  62  63 
##   1   1   2   3   1   2   2   1   1   2   1   2   1   2   3   1   2   1   1   2 
##  65  66  67  68  69  70  72  73  74  75  76  77  79  80  81  83  84  85  86  87 
##   3   1   1   2   3   2   2   2   1   2   1   1   3   2   2   1   2   2   2   1 
##  88  89  90  91  92  93  94  95  96  97  98  99 100 101 103 104 105 106 107 108 
##   2   1   1   3   2   1   2   3   2   1   1   1   2   2   2   2   2   2   2   2 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   1   2   1   2   1   1   2   3   1   2   2   3   3   2   2   2 
## 129 131 132 133 134 135 136 138 139 140 141 143 144 145 146 147 149 150 151 152 
##   2   3   2   2   1   1   1   1   2   2   1   3   2   2   2   2   1   1   3   2 
## 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 173 174 
##   2   2   2   3   1   2   3   1   1   2   3   1   1   2   2   3   1   1   2   1 
## 175 177 178 179 180 181 182 183 185 186 187 188 190 191 193 194 195 196 197 198 
##   2   1   2   2   1   2   2   2   2   2   2   1   1   1   2   1   1   1   3   1 
## 199 200 201 202 203 204 206 208 209 210 211 212 214 215 216 217 218 219 220 221 
##   3   3   1   1   2   2   2   2   2   1   1   1   3   1   3   2   1   3   2   1 
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
##   1   2   3   2   3   3   1   2   2   3   1   1   1   2   2   2   2   2   1   3 
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 
##   3   1   1   1   2   3   1   2   2   3   2   2   2   2   2   1   2   3   1   1 
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 
##   1   2   3   1   1   2   2   2   3   3   1   2   3   2   1   2   2   3   2   3 
## 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 
##   2   3   3   1   2   1   1   3   3   2   2   2   2   1   2   1   2   2   2   1 
## 302 303 305 306 307 308 309 310 311 312 314 315 316 317 318 319 320 321 322 323 
##   1   2   1   1   2   3   2   2   1   2   1   1   2   2   1   1   3   1   2   1 
## 324 326 327 328 329 330 331 332 334 338 339 340 341 342 343 344 345 346 348 349 
##   2   2   2   2   1   1   3   3   3   1   2   1   1   2   2   3   2   2   1   1 
## 350 351 352 353 355 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 
##   2   2   3   2   2   1   1   1   1   2   2   2   2   2   3   2   1   1   2   1 
## 372 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 
##   1   2   1   2   2   2   2   2   1   2   1   1   2   2   2   3   2   1   1   2 
## 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 410 411 412 413 
##   1   1   3   2   2   2   2   1   1   1   2   1   1   2   1   2   3   3   2   1 
## 414 415 416 417 418 419 420 421 423 424 425 426 427 428 429 431 432 433 434 435 
##   2   1   1   3   2   3   1   1   1   3   1   2   1   2   1   2   1   1   2   2 
## 436 438 439 440 441 443 444 445 446 448 449 450 451 452 453 454 455 456 
##   2   1   2   2   1   1   2   2   2   1   2   2   1   1   2   3   1   3
View(nbclust_obj$Best.nc)

#Both methods show that 3 is the optimal number of clusters!
#Run algorithm agian with 3 centers.
set.seed(1)
kmeans_obj = kmeans(clust_nba, centers = 3, 
                        algorithm = "Lloyd")

kmeans_obj
## K-means clustering with 3 clusters of sizes 156, 182, 80
## 
## Cluster means:
##   minutes_played     threes     points
## 1      0.1612805 0.06457161 0.07613553
## 2      0.5231444 0.22428303 0.27985871
## 3      0.7928070 0.47603659 0.60495238
## 
## Clustering vector:
##   1   2   3   4   5   6   7   9  10  11  12  13  14  15  16  17  18  19  20  21 
##   2   2   1   1   1   2   2   2   1   1   1   1   1   1   1   2   2   1   3   2 
##  22  23  24  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 
##   2   3   1   2   1   1   3   1   2   2   2   2   1   2   3   2   1   3   2   2 
##  43  44  45  46  47  48  49  50  51  52  53  54  55  57  58  59  60  61  62  63 
##   1   1   2   3   1   2   2   1   1   3   1   3   1   2   3   1   2   1   1   2 
##  65  66  67  68  69  70  72  73  74  75  76  77  79  80  81  83  84  85  86  87 
##   3   1   1   2   3   2   2   2   1   2   1   1   3   2   2   1   2   2   3   1 
##  88  89  90  91  92  93  94  95  96  97  98  99 100 101 103 104 105 106 107 108 
##   2   1   1   3   2   1   2   3   2   1   1   1   2   2   3   2   2   2   2   3 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   2   2   1   2   1   1   2   3   1   2   3   3   3   3   2   2 
## 129 131 132 133 134 135 136 138 139 140 141 143 144 145 146 147 149 150 151 152 
##   2   3   2   2   1   1   1   1   2   2   1   3   3   2   2   2   1   1   3   2 
## 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 173 174 
##   2   2   2   3   1   2   3   1   1   2   3   1   2   2   2   3   1   1   2   1 
## 175 177 178 179 180 181 182 183 185 186 187 188 190 191 193 194 195 196 197 198 
##   2   1   2   2   1   2   2   2   2   2   2   1   1   1   2   1   1   1   3   2 
## 199 200 201 202 203 204 206 208 209 210 211 212 214 215 216 217 218 219 220 221 
##   3   3   1   1   2   2   2   2   2   1   1   1   3   1   3   2   2   3   2   1 
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
##   1   2   3   2   3   3   1   3   2   3   1   1   1   2   2   2   2   2   1   3 
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 
##   3   1   1   1   2   3   1   2   3   3   2   2   2   2   2   1   2   3   1   1 
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 
##   1   2   3   1   1   2   2   3   3   3   1   2   3   2   1   2   2   3   2   3 
## 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 
##   2   3   3   1   2   1   1   3   3   2   2   2   2   1   2   1   2   2   2   1 
## 302 303 305 306 307 308 309 310 311 312 314 315 316 317 318 319 320 321 322 323 
##   1   2   1   1   2   3   3   2   1   2   1   1   2   2   1   1   3   1   2   1 
## 324 326 327 328 329 330 331 332 334 338 339 340 341 342 343 344 345 346 348 349 
##   2   2   2   2   1   1   3   3   3   1   2   1   1   2   2   3   2   2   1   1 
## 350 351 352 353 355 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 
##   2   3   3   2   2   1   1   1   1   2   2   2   2   2   3   2   1   1   2   1 
## 372 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 
##   1   2   1   3   2   2   2   3   1   2   1   1   2   2   2   3   2   1   1   2 
## 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 410 411 412 413 
##   1   1   3   2   2   2   2   1   1   1   2   1   1   2   2   2   3   3   2   1 
## 414 415 416 417 418 419 420 421 423 424 425 426 427 428 429 431 432 433 434 435 
##   2   1   1   3   2   3   1   1   1   3   1   2   1   2   1   2   1   1   2   2 
## 436 438 439 440 441 443 444 445 446 448 449 450 451 452 453 454 455 456 
##   2   1   3   2   1   1   2   2   2   1   2   2   1   1   2   3   1   3 
## 
## Within cluster sum of squares by cluster:
## [1] 2.444779 6.621360 5.534951
##  (between_SS / total_SS =  76.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
#now the ratio is 76.4% - much better.
#Visualizing output
salary_clusters <- as.factor(kmeans_obj$cluster)
#salary_clusters

ggplot(nba, aes(x = minutes_played, 
                            y = threes,
                            color = salary,  #<- tell R how to color 
                            #   the data points
                            shape = salary_clusters)) + 
  geom_point(size = 6) +
  ggtitle("Threes Scored vs Minutes Played for NBA Players") +
  xlab("Number of Minutes Played") +
  ylab("Number of Threes Scored") +
  scale_shape_manual(name = "Cluster", 
                     labels = c("Cluster 1", "Cluster 2", "Cluster 3"),
                     values = c("1", "2", "3")) +
  #scale_color_manual(name = "Salary",         #<- tell R which colors to use and
                     #   which labels to include in the legend
                    # labels = c("Lowest", "Low", "High", "Highest"),
                     #values = c("red", "orange", "yellow", "green")) +
  theme_light()

#Bonus: Create a 3d version of the output
#no need to join datasets/define colors for different salaries 
#because we are treating salary as a continuous variable!
#color will be a gradient

#adds the cluster column
nba$clusters <- (salary_clusters)

#removes characters that aren't going to be parseable
nba$Player <- gsub("[^[:alnum:]]", "", nba$Player)

# Use plotly to do a 3d imaging 
fig <- plot_ly(nba, 
               type = "scatter3d",
               mode="markers",
               symbol = ~clusters,
               x = ~minutes_played, 
               y = ~threes, 
               z = ~points,
               color = ~salary, # ~ means "identify just this variable and use all layers (plotly)
               text = ~paste('Player: ',Player,
                             "Salary: ", salary))

fig

In this document I have analyzed salary and performance measures of different NBA players to ultimately determine whether players are generally over- or under-paid. After close examination using k-means clustering with the variables of minutes played, threes scored, overall points scored, and salary, it is clear that NBA players are generally underpaid. This is clearly shown in the first plot, as we can see that there are not many light blue points on towards the origin, but there are several darker points as the graph approaches infinity.

Using the 3d plot, I was able to identify three players I believe would be best to add to our team. The following players would be good additions to our team because they perform well but are currently being paid low salaries. Thus, it would not be difficult to offer them a better salary and improve our team. The players are: Donovan Mitchell, DeAaron Fox, and Trae Young.